C:\Users\John\Google Drive\laptop_work\ece538\jhtp_10th\ch07\fig07_24>java ArrayListCollection Display list contents with counter-controlled loop: yellow red%nDisplay list contents with enhanced for statement: yellow red List with two new elements: yellow red green yellow Remove first instance of yellow: red green yellow Remove second list element (green): red yellow "red" is in the list Size: 2
ArrayListCollection.java
// Fig. 7.24: ArrayListCollection.java // Generic ArrayList collection demonstration. import java.util.ArrayList; public class ArrayListCollection { public static void main(String[] args) { // create a new ArrayList of Strings with an initial capacity of 10 ArrayList<String> items = new ArrayList<String>(); items.add("red"); // append an item to the list items.add(0, "yellow"); // insert "yellow" at index 0 // header System.out.print( "Display list contents with counter-controlled loop:"); // display the colors in the list for (int i = 0; i < items.size(); i++) System.out.printf(" %s", items.get(i)); // display colors using enhanced for in the display method display(items, "%nDisplay list contents with enhanced for statement:"); items.add("green"); // add "green" to the end of the list items.add("yellow"); // add "yellow" to the end of the list display(items, "List with two new elements:"); items.remove("yellow"); // remove the first "yellow" display(items, "Remove first instance of yellow:"); items.remove(1); // remove item at index 1 display(items, "Remove second list element (green):"); // check if a value is in the List System.out.printf("\"red\" is %sin the list%n", items.contains("red") ? "": "not "); // display number of elements in the List System.out.printf("Size: %s%n", items.size()); } // display the ArrayList's elements on the console public static void display(ArrayList<String> items, String header) { System.out.print(header); // display header // display each element in items for (String item : items) System.out.printf(" %s", item); System.out.println(); } } // end class ArrayListCollection
Maintained by John Loomis, updated Tue Jan 24 18:04:28 2017